package com.bw.house.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bw.house.dao.HouseRentDao;
import com.bw.house.entity.HouseRentEntity;
import com.bw.house.service.HouseRentService;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;


@Service("houseRentService")
public class HouseRentServiceImpl extends ServiceImpl<HouseRentDao, HouseRentEntity> implements HouseRentService {


    @SuppressWarnings("all")
    @Autowired
    private RestHighLevelClient client;



    @Override
    public PageUtils queryPage(Map<String, Object> params) throws IOException {
        SearchRequest searchRequest = new SearchRequest("zufang");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        String start = (String) params.get("start");
        String end = (String) params.get("end");
        if (StringUtils.isNotEmpty(start)){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("housePrice").gte(start));  //租房价格大于start
        }
        if (StringUtils.isNotEmpty(end)){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("housePrice").lte(end));  //租房价格小于于start
        }
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("limit");

        searchSourceBuilder.size((page-1)*size);
        searchSourceBuilder.from(page);
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        ArrayList<HouseRentEntity> rentList = new ArrayList<>();
        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
        long numHits = totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            HouseRentEntity houseRentEntity = JSON.parseObject(sourceAsString, new TypeReference<HouseRentEntity>() {
            });
            houseRentEntity.setRentId(Long.parseLong(id));
            rentList.add(houseRentEntity);
        }
        //List<?> list, int totalCount, int pageSize, int currPage
        return new PageUtils(rentList, (int) numHits,page,size);
    }

    @Override
    public PageUtils zuFangEsList(Map<String, Object> params) throws IOException {
        return null;
    }

}